class TorrentController < ApplicationController
  
  before_filter :login_required
  
  def index
		if @user != nil
			@torrents = @user.torrents
			@t_status = Hash.new
			for t in @torrents
				@t_status[t.id.to_s] = t.check_status
			end
		else 
			redirect_to :controller => 'user', :action => 'login'
		end
  end
  
  def new
  	@torrent = Torrent.new
		if request.post?
			if User.find(@user).admin and !params[:new][:uid]
				do_new params[:new][:uid], params[:new][:torrent]
			else
				do_new @user, params[:new][:torrent]
			end
		end
  end
  
  def do_new(u_id, torrent)
  	@user = User.find u_id
		transmission_res = @torrent.store(torrent, @user.id.to_s)
		transmission_id = transmission_res[:transmission_id]
		t_hash = transmission_res[:t_hash]
		dup_tor = transmission_res[:dup_tor]
		if transmission_id == -1
			#handle error
			flash[:notice]='Unexpected Error'
			redirect_to :action => 'index'
		elsif dup_tor == "false"
			#torrent is new, create a new tuple to insert into the db
			@torrent = Torrent.new :name => torrent.original_filename.to_s, :transmission_id => transmission_id, :t_hash => t_hash
		elsif dup_tor == "true"
			#torrent is a duplicate, find it inside the db
			@torrent = Torrent.where("t_hash = ?", t_hash)
			if @torrent.count == 1
				@torrent = @torrent.first
			else
				flash[:notice] = 'Unexpected Error'
				redirect_to :action => 'index'
			end
		end
		logger.debug @torrent
		#handle db
		if dup_tor == "true"
			if !@torrent.users.exists?(@user)
				if @torrent.users << @user
					flash[:notice]='Torrent Uploaded Correctly'
					redirect_to :action => 'index'
				else
					flash[:notice]='Error Uploading Torrent'
				end
			else
				flash[:notice] = 'You Already Have Uploaded This Torrent!'
				redirect_to :action => 'index'
			end
		else
			if @torrent.save
				if @torrent.users << @user
					flash[:notice]='Torrent Uploaded Correctly'
					redirect_to :action => 'index'
				end
			else
				flash[:notice] = 'Error Uploading Torrent'
			end
		end
  end
  
  def destroy
  	if User.find(@user).admin and params[:uid]
  		do_destroy params[:id], params[:uid]
  	else
  		@t = Torrent.find params[:id]
  		if @t.users.find @user
  			do_destroy params[:id], @user
  		else
  			flash[:notice] = 'Security Violation!'
  			redirect_to :root
  		end
  	end
  end
  
  def do_destroy(id, u_id)
		if destroy_torrent(id, u_id) == 'UnErr'
			flash[:notice] = 'More Than One Torrent With The Same ID!'
			redirect_to :action => 'index'
		else
			flash[:notice] = 'Torrent Successfully Deleted'
			redirect_to :action => 'index'
		end
  end
  
  def download
		id = dl_torrent params[:id], session[:user_id]
		if id == -1
			flash[:notice] = 'Error Downloading Torrent'
		else 
			flash[:notice] = 'Download Started' 
			@torrent.update_attributes({:transmission_id => id})
		end
		redirect_to :action => 'index'
  end

  def retrieve
		if !ret_torrent params[:id]
				flash[:notice] = 'Torrent Is Still Downloading'
				redirect_to :action => 'index'	
		end
  end

end

